import 'package:flutter/material.dart';

/**
 *
 * @ProjectName:    sk_base
 * @Package:        router_manager
 * @ClassName:      router_manager
 * @Description:    路由展馆里文件
 * @Author:         孙浩
 * @CreateDate:     5/22/21 3:32 PM
 */

class SKRouterManager extends RouteObserver {
  bool logRouter = true;
  Route routerLock; //定义锁定router

  List<Route> routerList = []; //router 栈管理列表

  @override
  void didPop(Route route, Route previousRoute) {
    routerList.removeLast();
    logPrint(
        "pop router name : ${route.settings.name} , routerList.length:${routerList.length} , routerListInfo is :${routerList}");
    super.didPop(route, previousRoute);
  }

  @override
  void didPush(Route route, Route previousRoute) {
    if (route is PageRoute) {
      //只有页面路由才会进入栈管理
      routerList.add(route);
      logPrint("push router name : ${route.settings.name}");
    }
    if (routerLock == null) {
      //防止没有初始值，进行初始定义
      routerLock = routerList.first;
    }
    super.didPush(route, previousRoute);
  }

  @override
  void didRemove(Route route, Route previousRoute) {
    super.didRemove(route, previousRoute);
  }

  @override
  void didReplace({Route newRoute, Route oldRoute}) {
    if (newRoute is PageRoute) {
      routerList.last = newRoute;
    }
    super.didReplace(newRoute: newRoute, oldRoute: oldRoute);
  }

  logPrint(String message) {
    if (logRouter) {
      print(message);
    }
  }

  unLockRouter() {
    //敏感操作后重新设置锁定router为起始路由
    routerLock = routerList.first;
  }

  lockRouter() {
    //锁定记录router
    routerLock = routerList.last;
  }
}
